package com.zpark.hystrix;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringCloudApplication
@EnableEurekaClient
@EnableFeignClients
// 在启动类上添加 @EnableHystrix 或者 @EnableCircuitBreaker。
// 注意，@EnableHystrix 中包含了 @EnableCircuitBreaker
// 启用Hystrix
@EnableHystrix
// 声明启用可视化数据监控
@EnableHystrixDashboard
public class Hystrix03Application {
    public static void main(String[] args) {
        SpringApplication.run(Hystrix03Application.class, args);
    }

    /**
     * 牢
     * *此配置是为了服务监控而配置，与服务容错本身无关，springcLoud升级后的坑*ServletRegistrationBean
     * 因为springboot的默认路径不是"/hystrix.stream"*只要在自己的项目里配置上下面的servLet就可以了
     * @return
     */
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet hystrix=new HystrixMetricsStreamServlet();
        ServletRegistrationBean bean=new ServletRegistrationBean(hystrix);
        bean.setLoadOnStartup(1);
        bean.addUrlMappings("/hystrix.stream");
        bean.setName("HystrixMetricsStreamServlet");
        return bean;
    }
}
